package cn.demomaster.quickui_sample.ui.fragment.component;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.demomaster.quickcompat.widget.layout.VisibleLayout;

import java.util.Arrays;
import java.util.List;

import cn.demomaster.quickrouter.fragment.QuickFragment;
import cn.demomaster.quickui.annotation.ActivityPager;
import cn.demomaster.quickui.annotation.ResType;
import cn.demomaster.quickui_sample.R;
import cn.demomaster.quickui_sample.adapter.VisiableLayoutAdapter;
import cn.demomaster.quickui.view.timeline.TimeLineView;


/**
 * Squirrel桓
 * 2018/8/25
 */

@ActivityPager(name = "Framelayout", preViewClass = TextView.class, resType = ResType.Custome)
public class FramelayoutFragment extends QuickFragment {

    RecyclerView recyclerView;
    TextView btn_show;

    @NonNull
    @Override
    public View onGenerateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fragment_layout_framelayout, null);
        return mView;
    }

    public void initView(View rootView) {
        recyclerView = rootView.findViewById(R.id.recycler_view);
        btn_show = rootView.findViewById(R.id.btn_show);
        btn_show.setOnClickListener(v -> {
            View view = rootView.findViewById(R.id.tv_content);
            if (view.getVisibility() == View.VISIBLE) {
                rootView.findViewById(R.id.tv_content).setVisibility(View.GONE);
                btn_show.setText("显示");
            } else {
                rootView.findViewById(R.id.tv_content).setVisibility(View.VISIBLE);
                btn_show.setText("隐藏");
            }
        });

        VisibleLayout vl_layout = rootView.findViewById(R.id.vl_layout);
        RadioGroup rg_positon = rootView.findViewById(R.id.rg_positon);
        rg_positon.setOnCheckedChangeListener((group, checkedId) -> {
            if(checkedId == R.id.btn_left) {
                vl_layout.setGravity(Gravity.LEFT);
            }else if(checkedId == R.id.btn_right){
                    vl_layout.setGravity(Gravity.RIGHT);
            }else if(checkedId == R.id.btn_top){
                    vl_layout.setGravity(Gravity.TOP);
            }else if(checkedId == R.id.btn_bottom){
                    vl_layout.setGravity(Gravity.BOTTOM);
            }
        });

        List data = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18");
        VisiableLayoutAdapter recycleAdapter = new VisiableLayoutAdapter(getContext(), data);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        //设置Adapter
        recyclerView.setAdapter(recycleAdapter);
        //添加动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置分隔线
        //recyclerView.addItemDecoration(new DividerGridItemDecoration(this));
        //添加分割线
        //mRecyclerView.addItemDecoration(new RefreshItemDecoration(getContext(), RefreshItemDecoration.VERTICAL_LIST));
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        ((TimeLineView) rootView.findViewById(R.id.time_line)).setTargetViewById(recyclerView, R.id.tv_message);
    }

}